<template>
	<div class="password-field">
		<input
			id="password"
			class="input"
			name="password"
			:placeholder="$t('user.auth.passwordPlaceholder')"
			required
			:type="passwordFieldType"
			autocomplete="current-password"
			:tabindex="tabindex"
			@keyup.enter="e => $emit('submit', e)"
			@focusout="() => {validate(); validateAfterFirst = true}"
			@keyup="() => {validateAfterFirst ? validate() : null}"
			@input="handleInput"
		>
		<BaseButton
			v-tooltip="passwordFieldType === 'password' ? $t('user.auth.showPassword') : $t('user.auth.hidePassword')"
			class="password-field-type-toggle"
			:aria-label="passwordFieldType === 'password' ? $t('user.auth.showPassword') : $t('user.auth.hidePassword')"
			@click="togglePasswordFieldType"
		>
			<Icon :icon="passwordFieldType === 'password' ? 'eye' : 'eye-slash'" />
		</BaseButton>
	</div>
	<p
		v-if="isValid !== true"
		class="help is-danger"
	>
		{{ isValid }}
	</p>
</template>

<script lang="ts" setup>
import {ref, watch} from 'vue'
import {useDebounceFn} from '@vueuse/core'
import {useI18n} from 'vue-i18n'
import BaseButton from '@/components/base/BaseButton.vue'

const props = defineProps({
	tabindex: String,
	modelValue: String,
	// This prop is a workaround to trigger validation from the outside when the user never had focus in the input.
	validateInitially: Boolean,
	validateMinLength: {
		type: Boolean,
		default: true,
	},
})
const emit = defineEmits(['submit', 'update:modelValue'])
const {t} = useI18n()
const passwordFieldType = ref('password')
const password = ref('')
const isValid = ref<true | string>(props.validateInitially === true ? true : '')
const validateAfterFirst = ref(false)

watch(
	() => props.validateInitially,
	() => props.validateInitially && validate(),
	{immediate: true},
)

const validate = useDebounceFn(() => {
	if (password.value === '') {
		isValid.value = t('user.auth.passwordRequired')
		return
	}

	if (props.validateMinLength && password.value.length < 8) {
		isValid.value = t('user.auth.passwordNotMin')
		return
	}
	
	if (props.validateMinLength && password.value.length > 250) {
		isValid.value = t('user.auth.passwordNotMax')
		return
	}
	
	isValid.value = true
}, 100)

function togglePasswordFieldType() {
	passwordFieldType.value = passwordFieldType.value === 'password'
		? 'text'
		: 'password'
}

function handleInput(e: Event) {
	password.value = (e.target as HTMLInputElement)?.value
	emit('update:modelValue', password.value)
}
</script>

<style scoped>
.password-field {
	position: relative;
}

.password-field-type-toggle {
	position: absolute;
	color: var(--grey-400);
	top: 50%;
	right: 1rem;
	transform: translateY(-50%);
}
</style>